package com.example.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author zhaoyunlong
 * @date 2020/11/11
 * @description
 */
public class StreamDemo {

    public static void main(String[] args) {
        /***
         * Stream 生成操作
         */
        arrays();
        collection();
        generate();
        iterate();
        chars();

        /**
         * 中间操作
         */

        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        list.stream().filter(x -> x % 2 == 0).forEach(System.out::println);

        /**
         * 终止操作
         */
        List<Integer> list2 = Arrays.asList(1, 2, 3, 4);
        OptionalInt max = list2.stream().filter(x -> x % 2 == 0).mapToInt(x -> x).max();
        System.out.println(max.getAsInt());

        int sum = list2.stream().filter(x -> x % 2 == 0).mapToInt(x -> x).sum();
        System.out.println(sum);

        Optional<Integer> max1 = list2.stream().filter(x -> x % 2 == 0).max((a, b) -> a - b);
        System.out.println(max1.get());


        /**
         * 将集合中的元素进行过滤的同时，仍然返回一个集合对象
         */
        List<Integer> collect = list2.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
        collect.forEach(System.out::println);

        /**
         * 去重
         */
        list2.stream().distinct().forEach(System.out::println);
        list2.stream().collect(Collectors.toSet()).forEach(System.out::println);

        /**
         * 打印 20----30 这样的集合数据
         */
        Stream.iterate(1, x->x+1).limit(50).skip(20).limit(10).forEach(System.out::println);
        String str = "11,12,45,23";
        int sum1 = Stream.of(str.split(",")).mapToInt(s -> Integer.valueOf(s)).sum();
        int sum2 = Stream.of(str.split(",")).mapToInt(Integer::valueOf).sum();
        int sum3 = Stream.of(str.split(",")).map(Integer::valueOf).mapToInt(x->x).sum();
        System.out.println(sum1);
        System.out.println(sum2);
        System.out.println(sum3);

        /**
         * 创建自定义对象
         */

        String name = "java, scala, python";
        Stream.of(name.split(",")).map(s -> new Person(s)).forEach(System.out::println);
        Stream.of(name.split(",")).map(Person::new).forEach(System.out::println);
        Stream.of(name.split(",")).map(Person::build).forEach(System.out::println);




    }

    private static void arrays() {
        String[] strs = {"a", "b", "c", "d"};
        Stream<String> strs1 = Stream.of(strs);
        strs1.forEach(System.out::println);
    }


    static void collection() {
        List<String> list = Arrays.asList("1", "2", "3", "4");
        Stream<String> stream = list.stream();
        stream.forEach(System.out::println);
    }

    static void generate() {
        // generate是一个无限循环执行的意思，此时会不断地输出1
        Stream<Integer> generate = Stream.generate(() -> 1);
        // 添加limit意思限制10次
        generate.limit(10).forEach(System.out::println);
    }

    static void iterate(){
        Stream<Integer> iterate = Stream.iterate(1, (x) -> x = x + 1);
        iterate.limit(10).forEach(System.out::println);
    }

    static void chars(){
        String str = "abcdef";
        IntStream chars = str.chars();
        chars.forEach(System.out::println);

    }
}
